package com.leo.employ.config

import org.mybatis.spring.SqlSessionFactoryBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import javax.sql.DataSource


@Configuration
class MybatiesConfig {
    @Bean
    @ConditionalOnMissingBean
    fun sqlSessionFactory(dataSource: DataSource): SqlSessionFactoryBean {
        val sqlSessionFactoryBean = SqlSessionFactoryBean()
        // 设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource)
        // 设置mybatis的主配置文件
        val resolver = PathMatchingResourcePatternResolver()
        val mybatisConfigXml = resolver.getResource("classpath:mybatis/SqlMapConfig.xml")
        sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml)
        // 设置别名包
        sqlSessionFactoryBean.setTypeAliasesPackage("cn.leo.employ.pojo")

        return sqlSessionFactoryBean
    }
}